home *** CD-ROM | disk | FTP | other *** search
- _MASM'S CHANGING FACE_
- by Mike Schmit
-
-
-
- [LISTING ONE]
-
- .MODEL small
- .STACK 100 ; reserves 100 bytes for the stack
- .CODE ; start of code segment
- main PROC
- .STARTUP ; generates startup code
- mov bx, 1 ; stdout
- mov cx, msg_len
- mov dx, offset DGROUP:msg
- mov ah, 40h ; write to handle
- int 21h ; call DOS to write msg
- .EXIT ; generates exit code
- main ENDP
- .DATA ; start of data segment
- msg BYTE 'Hello world.'
- msg_len equ $ - msg
- END main ; end, specify starting address
-
-
-
-
-
- [LISTING TWO]
-
-
- EXTRN GetDC : far
- EXTRN MoveTo : far
- EXTRN LineTo : far
- EXTRN ReleaseDC : far
-
- point_list struc
- x1 dw ?
- y1 dw ?
- x2 dw ?
- y2 dw ?
- point_list ends
- .
- . (assume bx = hWnd)
- .
- push bx
- call GetDC ; returns hDC
- mov di, ax
-
- push di
- push [si].x1
- push [si].y1
- call MoveTo
-
- push di
- push [si].x2
- push [si].y2
- call LineTo
-
- push bx
- push di
- call ReleaseDC
- .
- .
- .
-
-
-
- [LISTING THREE]
-
- GetDC PROTO FAR PASCAL hWnd:WORD
- MoveTo PROTO FAR PASCAL hDC:WORD, nX:WORD, nY:WORD
- LineTo PROTO FAR PASCAL hDC:WORD, nX:WORD, nY:WORD
- ReleaseDC PROTO FAR PASCAL hWnd:WORD, hDC:WORD
-
- option oldstructs
- point_list struct
- x1 word ?
- y1 word ?
- x2 word ?
- y2 word ?
- point_list ends
- .
- . (assume bx = hWnd)
- .
- invoke GetDC, bx ; returns hDC
- mov di, ax
- invoke MoveTo, di, [si].x1, [si].y1
- invoke LineTo, di, [si].x2, [si].y2
- invoke ReleaseDC, bx, di
- .
- .
- .
-
-
-
-
-
-
- [LISTING FOUR]
-
- factorial MACRO num
- LOCAL result, factor
- IF num LE 0
- %error factorial parameter out of bounds
- ENDIF
- result = 1
- factor = num
- WHILE factor GT 0
- result = result * factor
- factor = factor - 1
- ENDM
- EXITM %result
- ENDM
- i = 1
- REPEAT 20 ; repeat block macro
- DWORD factorial(i) ; to generate a table of
- i = i + 1 ; the first 20 factorials
- ENDM
- DWORD factorial(-33) ; error
-
-
-
-
- Example 1. Macro parameters can either be required as designated
- by the REQ keyword or specify a default value
-
- set_cursor_pos MACRO row:REQ, col:REQ, page:=<0>
- mov dh, row
- mov dl, col
- mov bh, page
- int 10h
- ENDM
- ...
- set_cursor_pos 5, 10, 1 ; all parameters supplied
- ...
- set_cursor_pos 7, 15 ; page parameter takes default value
- ...
- set_cursor_pos ; ERROR: required parameters missing
-
-
-
-
- Figure 1: MASM 6.0 contains decision and loop directives (in this
- case, an .IF/.ELSE loop) that are translated to their
- corresponding instructions at assembly time.
-
- .IF ax < mem_word1
- mov mem_word2, 2
- .ELSE
- mov mem_word2, 3
- .ENDIF
-
- The above code is translated to the following:
-
- cmp ax, mem_word1
- jnb @C0001
- mov mem_word2, 2
- jmp @C0003
- @C0001:
- mov mem_word2, 3
- @C0003:
-
-
-
-
- Figure 2. MASM 6.0 automatically generates a jump fixup when
- there is a jump out of range. Notice this example that the
- generated code is five bytes long instead of two.
-
-
- cmp ax, error_code
- je exit_error
- db 128 dup(90h) ; (128 bytes of code, NOP's here)
- exit_error:
-
-
- MASM 6.0 translates this to the following:
-
-
- cmp ax, error_code
- jne $+3 ; Note: $+3 is a relative
- ; jump 3 bytes ahead
- jmp exit_error
- db 128 dup(90h)
- exit_error:
-
-
-